package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.zabbix.Zabbix;
import cn.sccl.dirms.monitor.model.Host;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 应用集
 * Created by liupan on 2017/3/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/application")
public class ApplicationController {


    /**
     * 获取某一个主机的应用集
     * @param hostid 主机id
     */
    @RequestMapping(value = "/get/{hostid}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> getItemByHostId(@PathVariable String hostid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output", "extend");
//        params.put("selectItems", "extend");
        params.put("hostids", hostid);
        Map<String, Object> result = zabbix.call("application.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        for (Map<String, Object> item : mapList) {
            item.put("id", item.get("applicationid"));
            item.put("state", "closed");
            item.put("type", 3);
        }
        return mapList;
    }

    /**
     * 获取所有交换机主机数据
     */
    @RequestMapping(value = "/getExchanger", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> getExchanger() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<>();
		params.put("selectHost", "extend");
        params.put("selectItems", "extend");
        Map<String, Object> filter = new HashMap<>();
        filter.put("name", "STATUS");
        params.put("filter",filter);
        Map<String, Object> result = zabbix.call("application.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        Map<String, Object> newResult = new HashMap<>();
        for (Map<String, Object> item : mapList) {
            newResult.put((String) item.get("hostid"), item.get("items"));
        }
        return newResult;
    }

    /**
     * 获取交换机接口入口流量数据
     */
    // @RequestMapping(value = "/getExchangerIncoming", method = {RequestMethod.POST,RequestMethod.GET})
    /*public Map<String, Object> getExchangerIncoming() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<>();
        params.put("selectHost", "extend");
        params.put("selectItems", "extend");
        Map<String, Object> filter = new HashMap<>();
        filter.put("name", "indata");
        params.put("filter",filter);
        Map<String, Object> result = zabbix.call("application.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        Map<String, Object> newResult = new HashMap<>();
        for (Map<String, Object> item : mapList) {
            newResult.put((String) item.get("hostid"), item.get("items"));
        }
        return newResult;
    }*/

    /**
     * 获取交换机接口出口流量数据
     */
    // @RequestMapping(value = "/getExchangerOutgoing", method = {RequestMethod.POST,RequestMethod.GET})
    /*public Map<String, Object> getExchangerOutgoing() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<>();
        params.put("selectHost", "extend");
        params.put("selectItems", "extend");
        Map<String, Object> filter = new HashMap<>();
        filter.put("name", "outdata");
        params.put("filter",filter);
        Map<String, Object> result = zabbix.call("application.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        Map<String, Object> newResult = new HashMap<>();
        for (Map<String, Object> item : mapList) {
            newResult.put((String) item.get("hostid"), item.get("items"));
        }
        return newResult;
    }*/

    /**
     * 查找主机elementid(hostid) --> ipaddr --> 端口信息
     */
    /*public Map<String, Object> getExchangerPortAndHostId(String coreExchangerIp) {
        CommonController commonController = new CommonController();
        // 所有主机host(ipaddr) --> item(hostid)
        Map allHostMap = commonController.listHost();

        // 获取所有主机流量
        ItemController itemController = new ItemController();
        Map<String, Object> inComingDataMap = itemController.getExchangerIncoming();
        Map<String, Object> outGoingDataMap = itemController.getExchangerOutgoing();

        // 查找ip---》交换机端口（port和interface）
        SnmpController snmpController = new SnmpController();
        Host host = new Host();
        host.setName(coreExchangerIp);
        Map<String, Object> exchangerIPAndPortMap = snmpController.getExchangeIPAndPort(host);
        Map<String, Object> resultMap = new HashMap<>();
        // 没有数据判断
        if (exchangerIPAndPortMap == null) {
            return resultMap;
        }
        for (String key: exchangerIPAndPortMap.keySet()) {
            Map<String,String> vmap = (Map<String, String>) exchangerIPAndPortMap.get(key);
            // String port = vmap.get("port");
            // String interfaceStr = vmap.get("interface");
            Map<String, String> hostItemMap = (Map<String, String>) allHostMap.get(key);
            if (hostItemMap != null) {
                String hostid = hostItemMap.get("hostid");
                if (hostid != null && !hostid.isEmpty()) {
                    Map<String,String> inComingDataItem = (Map<String, String>) inComingDataMap.get(hostid);
                    Map<String,String> outGoingDataItem = (Map<String, String>) outGoingDataMap.get(hostid);
                    String inCommingData = "", outGoingData = "";
                    if (inComingDataItem != null) {
                        // 入口流量值
                        inCommingData = inComingDataItem.get("lastvalue");
                    }
                    if (outGoingDataItem != null) {
                        // 出口流量值
                        outGoingData = outGoingDataItem.get("lastvalue");
                    }
                    vmap.put("incoming", inCommingData);
                    vmap.put("outgoing", outGoingData);
                    resultMap.put(hostid, vmap);
                }
            }
        }
        return resultMap;
    }*/
}
